package com.pinda.service.impl;

import com.alibaba.druid.sql.visitor.functions.Substring;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.base.entity.PdArea;
import com.pinda.mapper.PdAreaMapper;
import com.pinda.service.IPdAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.users.entity.PdAddressBook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@Service
public class PdAreaServiceImpl extends ServiceImpl<PdAreaMapper, PdArea> implements IPdAreaService {

    @Resource
    private PdAreaMapper pdAreaMapper;

    @Override
    public PdAddressBook getByCode(PdAddressBook pdAddressBook) {


        String code=pdAddressBook.getExtensionNumber();
        pdAddressBook.setExtensionNumber("");
        String code1= code.substring(0, 2) + "0000";
        String code2= code.substring(0, 4) + "00";
        String code3=code;

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("code",code1);
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("code",code2);
        QueryWrapper queryWrapper3 = new QueryWrapper();
        queryWrapper3.eq("code",code3);
        PdArea pdArea1 = this.pdAreaMapper.selectOne(queryWrapper1);
        PdArea pdArea2 = this.pdAreaMapper.selectOne(queryWrapper2);
        PdArea pdArea3 = this.pdAreaMapper.selectOne(queryWrapper3);
        pdAddressBook.setProvinceId(Long.valueOf(pdArea1.getId()));
        pdAddressBook.setCityId(Long.valueOf(pdArea2.getId()));
        pdAddressBook.setCountyId(Long.valueOf(pdArea3.getId()));
        if (!pdAddressBook.getAddress().contains("省")&&!pdAddressBook.getAddress().contains("市")){
            pdAddressBook.setAddress(pdArea1.getName()+pdArea2.getName()+pdArea3.getName()+pdAddressBook.getAddress());
        }


        return pdAddressBook;
    }
}
